<?php
class clsUploadFile 
{
	var $error;
	var $input_field_name;
	var $src_uploaded = "";
	var $maximum_size = 0;
	var $allowedExtensions = array("mp3","flv","mp4","mov","swf","pdf","jpeg","jpg","gif","png","bmp");
	function clsUploadFile($input_field_name, $maximum_size = 3145728)
	{
		$this->input_field_name = $input_field_name;
				
		if(isset($_POST['MAX_FILE_SIZE']))
		{
			$this->maximum_size = $_POST['MAX_FILE_SIZE'];
		}
		else
		{
			$this->maximum_size = $maximum_size;
		}
	}
	function isAllowedExtension($fileName)
	{
		if($this->allowedExtensions == NULL)
			return true;
		$file_ext = end(explode(".", $fileName));
		foreach($this->allowedExtensions as $ext)
		{
			if(strtolower($file_ext) == strtolower($ext))
				return true;
		}
		return false;
	}
	function allow_upload_file($upload_location)
	{
		$temp = 1024*1024;
		$convertMB = $this->maximum_size/$temp . "MB";
		if(!empty($_FILES[$this->input_field_name]['error']))
		{
			switch($_FILES[$this->input_field_name]['error'])
			{
				case '1':
					$this->error = "You have defined a maximum size for file uploads which exceeds the allowed size (".$convertMB.").";
					break;
				case '2':
					$this->error = 'The uploaded file exceeds the MAX_FILE_SIZE ('.$convertMB.') directive that was specified in the HTML form';
					break;
				case '3':
					$this->error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$this->error = 'No file was uploaded.';
					break;

				case '6':
					$this->error = 'Missing a temporary folder';
					break;
				case '7':
					$this->error = 'Failed to write file to disk';
					break;
				case '8':
					$this->error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$this->error = 'No error code avaiable';
			}
		}
		elseif(empty($_FILES[$this->input_field_name]['tmp_name']) || $_FILES[$this->input_field_name]['tmp_name'] == 'none')
		{
			$this->error = 'No file was uploaded..';
		}
		elseif(!$this->isAllowedExtension($_FILES[$this->input_field_name]['name']))
		{
			$this->error = 'Only files with the following extensions are allowed: ' . implode(", ", $this->allowedExtensions) . ' are accepted for upload';
		}
		elseif($_FILES[$this->input_field_name]["size"] == 0 || $_FILES[$this->input_field_name]["size"] > $this->maximum_size)
		{
			$this->error = "You have defined a maximum size for file uploads which exceeds the allowed size (".$convertMB.").";
		}
		//echo $upload_location 
		//check exist folder
		if(!$this->create_dir($upload_location))
			$this->error = "Cannot create folder: $create_folder";
		return $this->error != "" ? false: true;
	}
	function process_upload($upload_location, $input_field_name = "")
	{
		if($this->allow_upload_file($upload_location))
		{
			$uploadfile = $this->get_file_name($upload_location);
			
			if(@move_uploaded_file ( $_FILES[$this->input_field_name]['tmp_name'], $uploadfile))
			{
				@chmod($uploadfile , 0777);
				$this->src_uploaded = $uploadfile;
				return true;
			}
			else
			{
				foreach($_FILES as $items)
				{
					if($items['size'] == 0)
					{
						$this->error .= "<br>move_uploaded_file: You have defined a maximum size for file uploads which exceeds the allowed size (".($this->maximum_size/1048576)."MB).";
					}
					if($items['error'] != 0)
					{
						$this->error .= "<br>move_uploaded_file: cannot create tmp file";
					}
				}
			}
			//echo "<br><br><br>";
			//print_r($_FILES);
		}
		return false;
	}
	function delete_file($path, $rootSite = "", $rootFolder = "")
	{
	  if($path == "")
	  	return false;
	  if($rootSite != "" && $rootSite != "")	
		  $path = $rootSite . substr($path, strrpos($path, $rootFolder));
	  if(file_exists($path))
	  {
		if(@unlink($path))
			return true;
	  }
	  return false;
	}
	function isUpload()
	{
		return $_FILES[$this->input_field_name]['name'] != '' ? true: false;	
	}
	function get_file_name($upload_location)
	{
		$file_title = $_FILES[$this->input_field_name]['name'];
		
		$pathinfo = pathinfo(basename($file_title));
		$fileName = $pathinfo["filename"];
		$ext = $pathinfo["extension"];
		
		$fileName = $this->remove_special_character($fileName);
		$fileName = $upload_location . $fileName . '_' .rand(10000,99999);		
		/*
		if(file_exists($upload_location . $fileName . "." . $ext))
			$fileName = $upload_location . $fileName . '_' . date("Ymd");
		else
			$fileName = $upload_location . $fileName;
		*/	
		return $fileName . "." . $ext;
	}
	function create_dir($create_folder)
	{
		
		if($_SERVER['SERVER_NAME'] == "localhost")
		{
			if(!is_dir($create_folder))
			{
				if(!@mkdir($create_folder, 0777))
				{				
					return false;
				}
			}
			return true;
		}
		else
		{
			if(!is_dir($create_folder))
			{
				$create_folder = trim($create_folder, "/");
				$folderParent = substr($create_folder, 0, strrpos($create_folder, "/"));
				
				$child = explode("/", $create_folder);
				$dirname = $child[count($child)-1];
				//echo "<br>create_folder: ". $create_folder;
				//echo "<br>folderParent: ". $folderParent;
				//echo "<br>dirname: ". $dirname;
				
				if($folderParent == "" || $dirname == "")
					return false;
				
				define(DS, DIRECTORY_SEPARATOR);
				define(PATH_ROOT, DS."public_html" );
				
				define("FTP_SERVER","112.213.89.25");
				define("FTP_USERNAME","foodb2bt");
				define("FTP_PASSWORD","0906668453");
			
				$conn_id = ftp_connect(FTP_SERVER);  
				$login_result = ftp_login($conn_id, FTP_USERNAME, FTP_PASSWORD);
				ftp_chdir($conn_id, PATH_ROOT.DS.$folderParent);#DI DEN THU MUC NGUON
			
				if ( ftp_mkdir($conn_id, $dirname) )
				{
				 $dir= PATH_ROOT.DS.$folderParent.DS.$dirname;
				 @ftp_site($conn_id, "CHMOD 777 $dir");
				}  
				
				ftp_close($conn_id);
			}
			return true;
		}
		
	}
	function move_file($from, $to)
	{
		@rename($from, $to);
	}
	function remove_special_character($str)
	{
		$str = preg_replace('/[^a-zA-Z0-9]/', '', $str); 
		return $str;
	}
}
?>